using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace SharepointListMigrator
{
    public static class WebServiceHelper
    {
        private const string WEB_SERVICE_SUFFIX = "_vti_bin/Lists.asmx";

        public static Source.Lists.Lists GetSourceWebService(string sourceSiteUrl, System.Net.NetworkCredential networkCredentials)
        {
            if (string.IsNullOrEmpty(sourceSiteUrl)) 
                throw new ApplicationException("The source url site url must be supplied.");
            //TODO - test that the url is a valid url
            Source.Lists.Lists service = new Source.Lists.Lists();

            service.Url = sourceSiteUrl + (sourceSiteUrl[sourceSiteUrl.Length - 1] == '/' ? "" : "/") + WEB_SERVICE_SUFFIX;
            service.Credentials = networkCredentials;            //we have to pass creds thru coz our prod old server uses only basic auth.

            return service;
        }

        public static Destination.Lists.Lists GetDestinationWebService(string destinationSiteUrl, System.Net.NetworkCredential networkCredentials)
        {
            if (string.IsNullOrEmpty(destinationSiteUrl)) 
                throw new ApplicationException("The destination site url must be supplied.");
            //TODO - test that the url is a valid url
            Destination.Lists.Lists service = new Destination.Lists.Lists();

            service.Url = destinationSiteUrl + (destinationSiteUrl[destinationSiteUrl.Length - 1] == '/' ? "" : "/") + WEB_SERVICE_SUFFIX;
            service.Credentials = networkCredentials;  //we have to pass creds thru coz our prod old server uses only basic auth.

            return service;
        }

        public static XmlElement CreateUpdateListItemXMLWrapper(string destinationViewGuid)
        {
            System.Xml.XmlElement elBatch = new System.Xml.XmlDocument().CreateElement("Batch");

            elBatch.SetAttribute("OnError", "Continue");
            elBatch.SetAttribute("ListVersion", "1");
            elBatch.SetAttribute("ViewName", destinationViewGuid);

            return elBatch;
        }

        public static System.Net.NetworkCredential CreateCredentialToken(string username, string password, string domain)
        {
            if (string.IsNullOrEmpty(username) ||
                    string.IsNullOrEmpty(password) ||
                    string.IsNullOrEmpty(domain))
                throw new System.Security.SecurityException("Credentials must be supplied.");

            return new System.Net.NetworkCredential(username, password, domain);

        }


    }
}
